package com.kristin.thread.threadcreate;

import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class FutureMethod {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> future = new FutureTask<>(() -> {
            System.out.println("执行callable任务");
            return "callable返回值";
        });
        ExecutorService executor = Executors.newFixedThreadPool(1);
        executor.execute(future);
        while(true){
            if(future.isDone()) {
                System.out.println(future.get());
                break;
            }
        }

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
                System.out.println("执行callable任务");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "kristin";
        }, executor);
        System.out.println(completableFuture.get());
        System.out.println(completableFuture.join());
        System.out.println("提交任务完成");
        executor.shutdown();
    }

}
